/**
 *  编辑页
 *
 * 作者 admin
 * 日期 2024-08-20 15:49:12
 */
layui.use(['jquery','febs', 'form', 'validate','fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        fileUpload = layui.fileUpload,
        $view = $('#hazardExamineRectify-edit'),
        $hazardOperation = $view.find('#hazardOperation'),
        validate = layui.validate;

    let tenantId = POPUP_DATA.tenantId;
    let examineId = POPUP_DATA.examineId;
    let examineRecordId = POPUP_DATA.examineRecordId;
    let auditRecordList=[];

    initDictItemLabel_rectify();
    initFormValue();
    form.verify(validate);
    form.render();

    function initDictItemLabel_rectify(){
        febs.initDictItemLabel($hazardOperation,'SAFETY_ACCIDENT_TYPE',hazardExamineRectifyData.hazardOperation);
    }

    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'hazard/upload',
        type: 0,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: getRectifyFileList(),
        done: function (res) {
        },
    });
    function getRectifyFileList(){
        if (hazardExamineRectifyData.rectifyFileList && hazardExamineRectifyData.rectifyFileList.length>0) {
            return hazardExamineRectifyData.rectifyFileList;
        }else{
            return [];
        }
    }

    if (hazardExamineRectifyData.examineFileList && hazardExamineRectifyData.examineFileList.length>0) {
        var fileUploadProblem = fileUpload.render({
            elem: '#fileUploadProblem',
            url: '',
            type: 0,//0-图片，1-视频，2-其他
            maxNum:5,
            defaultData: hazardExamineRectifyData.examineFileList,
            isDetail: true,
            done: function (res) {
            },
        });
    }

    initRecordAuditTable();
    function initRecordAuditTable() {
        tableIns = febs.table.init({
            elem: $view.find("#recordAuditTable"),
            id: 'recordAuditTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'auditRealName', title: '审核人姓名', width: 150},
                {field: 'auditResult',align:'center', title: '审核结果', width: 100, templet: function (d) {
                        return d.auditResult =='1'?'审核通过':'审核退回';
                    }
                },
                {field: 'auditReason', title: '审核理由', minWidth: 260},
                {field: 'createdTime',align:'center', title: '审核时间', width: 180},
            ]],
            done: function(res, curr, count){
            },
            data:auditRecordList
        });
    }


    function initFormValue() {
        if (hazardExamineRectifyData) {
            if(hazardExamineRectifyData.auditRecordList && hazardExamineRectifyData.auditRecordList.length>0){
                auditRecordList = hazardExamineRectifyData.auditRecordList;
            }
            form.val("hazardExamineRectify-edit-form", {
                "examineRecordId": hazardExamineRectifyData.examineRecordId,
                "tenantId": hazardExamineRectifyData.tenantId,
                "examineId": hazardExamineRectifyData.examineId,
                "hazardExamineTitle": hazardExamineRectifyData.hazardExamineTitle,
                "hazardOperation": hazardExamineRectifyData.hazardOperation,
                "hazardLevel": hazardExamineRectifyData.hazardLevel,
                "areaId": hazardExamineRectifyData.areaId,
                "areaName": hazardExamineRectifyData.areaName,
                "examineEvidence": hazardExamineRectifyData.examineEvidence,
                "projectId": hazardExamineRectifyData.projectId,
                "projectName": hazardExamineRectifyData.projectName,
                "leaderDeptId": hazardExamineRectifyData.leaderDeptId,
                "leaderDeptName": hazardExamineRectifyData.leaderDeptName,
                "leaderId": hazardExamineRectifyData.leaderId,
                "leaderName": hazardExamineRectifyData.leaderName,
                "leaderNo": hazardExamineRectifyData.leaderNo,
                "questionDescription": hazardExamineRectifyData.questionDescription,
                "rectificationDeadline": hazardExamineRectifyData.rectificationDeadline,
                "rectificationRequire": hazardExamineRectifyData.rectificationRequire,
                "feedbackContent": hazardExamineRectifyData.feedbackContent,
                "feedbackDate": hazardExamineRectifyData.feedbackDate,
                "auditUserId": hazardExamineRectifyData.auditUserId,
                "auditRealName": hazardExamineRectifyData.auditRealName,
                "auditRemark": hazardExamineRectifyData.auditRemark,
                "auditDate": hazardExamineRectifyData.auditDate,
                "status": hazardExamineRectifyData.status,
                "recordUserId": hazardExamineRectifyData.recordUserId,
                "recordRealName": hazardExamineRectifyData.recordRealName,
                "recordDate": hazardExamineRectifyData.recordDate,
            });
            form.render('checkbox');
        }
    }


    form.on('submit(hazardExamineRectify-edit-form-submit)', function (data) {
        var rectifyResult =  $view.find("input[type=radio][name='rectifyResult']:checked").val();
        if(rectifyResult === "1"){
            //提交 检查记录状态置为待审核
            data.field.status = '4';
        }else{
            // 检查记录状态置为待整改指派
            data.field.status = '1';
        }
        addOrUpdate(data.field);
        return false;
    });

    form.on('radio(rectifyResult)',function (obj){
        if(obj.value == '1'){
            //已整改
            $('#rectification').removeClass('febs-hide');
            $('#reback').addClass('febs-hide');
            $view.find('textarea[name="returnReason"]').attr("lay-verify","");
            $view.find('textarea[name="feedbackContent"]').attr("lay-verify","required")
        }else {
            //退回
            $('#rectification').addClass('febs-hide');
            $('#reback').removeClass('febs-hide');
            $view.find('textarea[name="returnReason"]').attr("lay-verify","required")
            $view.find('textarea[name="feedbackContent"]').attr("lay-verify","")
        }

    })
    function addOrUpdate(data) {
        var fileList = getImgData();
        data.rectifyFileList=fileList;

        data.tenantId = tenantId;
        data.examineId = examineId;
        data.examineRecordId = examineRecordId;

        if (data.examineRecordId && data.examineRecordId.length > 0) {
            febs.modal.confirm('提交整改信息','确定提交整改信息？', function (index) {
                febs.postArray(ctx + 'hazardExamineRectify/rectify', data, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                    $('#febs-hazardExamineRectify').find('#query').trigger('click');
                });
            });

        }

    }

    function getImgData(){
        var imgDatas = $('#fileUpload').find(".imgOne")
        var imgList = [];
        $.each(imgDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
                smallPath: item.dataset.smallpath,
            }
            imgList.push(data);
        })
        return imgList;
    }

});